<?php
    $q = $_GET["q"];
    $a = mysqli_connect("localhost","root","gala123456");
    $db = mysqli_select_db($a,"sheep");
    $sql = "SELECT `business_name`,`img_url` FROM `business` WHERE `business_address` = '$q'";
    $query = mysqli_query($a,$sql);
    $address = array();
    $img_url = array();
    while ($row = mysqli_fetch_row($query)) {
        $ding = 0;
        foreach ($row as $nei) {
            if ($ding == 0) {
                array_push($address,$nei);
            }else if ($ding == 1) {
                array_push($img_url,$nei);
            }
            $ding++;
        }
    }
    $num_arr = array();
    for ($i = 0;$i < count($address);$i++) {
        $sql = "SELECT `business_sheepID` FROM `business` WHERE `business_name` = '".$address[$i]."'";
        $query = mysqli_query($a,$sql);
        while ($row = mysqli_fetch_row($query)) {
            foreach ($row as $nei) {
                $new_nei = explode("-",$nei);
                $num = 0;
                for ($j = 0;$j < count($new_nei);$j++) {
                    $sql = "SELECT `sheep_select` FROM `sheep` WHERE `sheepID` = '".$new_nei[$j]."'";
                    $query = mysqli_query($a,$sql);
                    while ($row2 = mysqli_fetch_row($query)) {
                        foreach ($row2 as $nei2) {
                            if ($nei2 != null) {
                                $num++;
                            }
                        }
                    }
                }
                array_push($num_arr,$num);
            }
        }
    }
    $arr = array(
        "address" => $address,
        "num_select" => $num_arr,
        "address_url" => $img_url
    );
    echo json_encode($arr);
?>